package com.controller.specialist;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.po.UserZJInfo;
import com.service.specialist.UserZJService;
import com.util.StringFinal;

@Controller
@RequestMapping("/jsp/specialist")
public class SpeciaController {
    @Resource(name = "userZJServiceImp")
    private UserZJService userZJService;

    @RequestMapping(value = "/getSpecialistview")
    public String getSpecialistView(Model model, HttpServletRequest request, HttpServletResponse resp) {
	int rows = 8;
	int page = 1;
	UserZJInfo userzjinfo = new UserZJInfo();
	userzjinfo.setRows(rows);
	userzjinfo.setPage((page - 1) * rows);
	List<UserZJInfo> list = userZJService.getUserZJList(userzjinfo);
	model.addAttribute("zjlist", list);
	return "specialist/specialists";
    }

    @ResponseBody
    @RequestMapping(value = "/getzjlist")
    public List<UserZJInfo> getSpecialists(int page, String type, String content) {
	// 每页显示几天数据为固定的
	// 每页显示6个数据
	int rows = 8;
	UserZJInfo userzjinfo = new UserZJInfo();
	if (type.equals("name"))
	    userzjinfo.setUserzj_name(content);
	if (type.equals("region"))
	    userzjinfo.setUserzj_region(content);
	userzjinfo.setRows(rows);
	userzjinfo.setPage((page - 1) * rows);
	List<UserZJInfo> list = userZJService.getUserZJList(userzjinfo);
	return list;
    }

    @ResponseBody
    @RequestMapping(value = "/getzjcount")
    public int getzjcount(String type, String content) {
	UserZJInfo userzjinfo = new UserZJInfo();
	if (type.equals("name"))
	    userzjinfo.setUserzj_name(content);
	if (type.equals("region"))
	    userzjinfo.setUserzj_region(content);
	int totalcount = userZJService.getUserZJCount(userzjinfo);
	return totalcount;
    }

    // 进入专家详情页面
    @RequestMapping(value = "/getZJDetail")
    public String getZJDetail(Model model, HttpServletRequest request, HttpServletResponse resp, String zjid)
	    throws IOException {
	UserZJInfo info = userZJService.getOneUserZJ(zjid);
	model.addAttribute("zj", info);
	return "specialist/expert";
    }

    // 进入专家个人中心页面
    @RequestMapping(value = "/getZJPersonalView")
    public String getZJInfoPersonal(Model model, HttpServletRequest request, HttpServletResponse resp)
	    throws IOException {
	UserZJInfo zjinfo = userZJService.getOneUserZJ(request.getSession().getAttribute("user_id").toString());
	model.addAttribute("experience_count",StringFinal.sp_experience_length-zjinfo.getUserzj_experience().length());
        model.addAttribute("result_count",StringFinal.sp_result_length-zjinfo.getUserzj_result().length());
	zjinfo.setUserzj_result(zjinfo.getUserzj_result().replace("&nbsp", " ").replace("<br>", "\n"));
	zjinfo.setUserzj_experience(zjinfo.getUserzj_experience().replace("&nbsp", " ").replace("<br>", "\n"));
	model.addAttribute("user", zjinfo);	
        return "infocenter/zjinfopersonal";
    }

    // 更新专家个人信息
    @ResponseBody
    @RequestMapping(value = "/updateUserZJ")
    public int updateUserZJ(UserZJInfo userzjinfo) {
	userzjinfo.setUserzj_experience(userzjinfo.getUserzj_experience().replace(" ", "&nbsp").replace("\r\n", "<br>"));
	userzjinfo.setUserzj_result(userzjinfo.getUserzj_result().replace(" ", "&nbsp").replace("\r\n", "<br>"));
	int i = userZJService.updateUserZJ(userzjinfo);
	return i;
    }

    /**
     * 修改专家头像
     * 
     * @param file
     * @param request
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/uploadZJImage")
    public Object getImage(@RequestParam("image") MultipartFile file, HttpServletRequest request) {

	int result = 0;
	String userid = request.getSession().getAttribute("user_id").toString();
	String filename = file.getOriginalFilename();
	String[] types = filename.split("\\.");
	String type = types[types.length - 1];
	String imagepath = request.getSession().getServletContext().getRealPath("/image/user/" + userid + "." + type);
	File file1 = new File(imagepath);
	StringBuffer buffer = new StringBuffer();
	buffer.append(userid).append(".").append(type);
	try {
	    file.transferTo(file1);
	    result = userZJService.updateUserZJImage(buffer.toString(), userid);
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	    result = 0;
	}
	return result;
    }
}
